﻿using EntityModel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class FavoritesController : ControllerBase
    {
        private readonly BitDbContext _db;
        public FavoritesController(BitDbContext db)
        {
            _db = db;
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Result GetFavor()
        {
            Result result = new Result();
            List<EM_Favorites> Favor= _db.EM_Favorite.ToList();
            if (Favor != null)
            {
                result.Code = 200;
                result.Data = Favor;
            }
            else
            {
                result.Code = 404;
            }
            result.Author = "";
            return result;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="eM_Favorites"></param>
        /// <returns></returns>
        [HttpPost]
        public Result AddFavor([FromBody] EM_Favorites eM_Favorites)
        {
            Result result = new Result();
            _db.EM_Favorite.Add(eM_Favorites);
            result.Data = _db.SaveChanges();
            return result;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public Result DelUser(Guid id)
        {
            Result result = new Result();
            EM_Favorites? user = _db.EM_Favorite.Where(w => w.Id == id).SingleOrDefault();
            if (user != null)
                _db.EM_Favorite.Remove(user);
            result.Data = _db.SaveChanges();
            return result;
        }
       /// <summary>
       /// 假删除
       /// </summary>
       /// <param name="Id"></param>
       /// <returns></returns>
       [HttpDelete("{Id}")]
        public bool Delete(Guid Id)
        {
           EM_Favorites? favorites = new EM_Favorites();
           favorites = _db.EM_Favorite.Where(s => s.Id == Id).FirstOrDefault();
           int result = 0;
           if (favorites != null)
           {
               favorites.DelTime = DateTime.Now;
               _db.EM_Favorite.Update(favorites);
                result = _db.SaveChanges();
           }
            return result > 0 ? true : false;
        }
    }
}
